import 'package:flutter/material.dart';
import 'package:read_book/data/book_mall/book_mall.dart';
import 'package:read_book/ui/widget/book_des.dart';

class Search extends StatefulWidget {
  const Search({Key key}) : super(key: key);

  @override
  SearchState createState() => SearchState();
}

class SearchState extends State<Search> {
  String searchValue;
  List bookList = [];

  void getSearch(String value) {
    BookMall().getSearch(value).then((value) {
      setState(() {
        bookList = value['books'];
      });
    });
  }

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: IconButton(
          icon: const Icon(Icons.keyboard_backspace),
          onPressed: () {
            Navigator.pop(context);
          },
          color: Colors.black54,
        ),
        backgroundColor: Colors.grey[50],
        title: SizedBox(
          height: 40.0,
          child: buildSearchInput(context),
        ),
        actions: <Widget>[
          IconButton(
            icon: const Icon(Icons.search),
            onPressed: () {
              getSearch(searchValue);
            },
            color: Colors.black54,
          )
        ],
      ),
      body: Center(
        child: ListView(
          children: <Widget>[
            Container(
              child: searchList(context),
            )
          ],
        ),
      ),
    );
  }

  ///输入框
  Widget buildSearchInput(BuildContext context) {
    final controller = TextEditingController();
    controller.text = searchValue;
    return TextField(
      maxLines: 1,
      autocorrect: false,
      autofocus: true,
      style: const TextStyle(fontSize: 16.0, color: Colors.black87),
      decoration: InputDecoration(
          fillColor: Colors.grey[200],
          filled: true,
          disabledBorder: InputBorder.none,
          enabledBorder: InputBorder.none,
          focusedBorder: InputBorder.none,
          hintText: '输入小说名称'),
      onSubmitted: (text) {
        getSearch(text);
        searchValue = text;
      },
      controller: controller,
    );
  }

  ///搜索补充列表
  Widget autoSearch() {
    return ListView.builder(
      shrinkWrap: true,
      physics: const NeverScrollableScrollPhysics(),
      itemCount: bookList.length,
      itemBuilder: (BuildContext context, int index) {
        return Container(
          padding: const EdgeInsets.only(left: 10.0),
          child: BookDes(book: bookList[index]),
          decoration: const BoxDecoration(
              border: Border(
                  bottom: BorderSide(width: 1, color: Color(0xffe5e5e5)))),
        );
      },
    );
  }

  ///列表
  Widget searchList(BuildContext context) {
    return ListView.builder(
      shrinkWrap: true,
      physics: const NeverScrollableScrollPhysics(),
      itemCount: bookList.length,
      itemBuilder: (BuildContext context, int index) {
        return Container(
          padding: const EdgeInsets.only(left: 10.0),
          child: BookDes(book: bookList[index]),
          decoration: const BoxDecoration(
              border: Border(
                  bottom: BorderSide(width: 1, color: Color(0xffe5e5e5)))),
        );
      },
    );
  }
}
